package cn.bxshey.webModule.base;

import cn.bxshey.commonModule.constant.JwtConstant;
import cn.bxshey.commonModule.enums.ResultEnum;
import cn.bxshey.commonModule.exception.BusinessException;
import cn.bxshey.commonModule.result.Result;
import cn.bxshey.utilModule.JwtUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;
import java.util.Map;
import java.util.Objects;

/**
 * @作者：不想说话而已
 * @Date：2024/3/10 2:14
 */
public class BaseController {

    //获取http信息
    public HttpServletRequest getHttpInfo() {
        return ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest();
    }

    //获取令牌包含的用户信息
    public Map<String, Object> getUserLoginInfo() {
        Map<String, Object> stringObjectMap = JwtUtil.parseToken(getHttpInfo().getHeader(JwtConstant.ACCESS_TOKENS));
        if (Objects.isNull(stringObjectMap)) {
            throw new BusinessException(ResultEnum.ERROR_LOGIN);
        }
        return stringObjectMap;
    }
}
